<?php
declare (strict_types=1);

namespace app\admin\controller\school;

use app\common\model\AdminRole;
use app\common\model\SchoolPettyCash;
use think\facade\Request;
use app\common\service\SchoolPettyCash as S;
use app\common\model\SchoolPettyCash as M;

class PettyCash extends \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    // 列表
    public function index()
    {
        $admin = session('admin');
        $users = dataFind('bl_admin_admin_role', ['admin_id'=> $admin['id']], '*', 'id desc');
        return $this->getAuto($this->fetch('', ['role_id' => $users['role_id']??1]), M::getList());
    }

    // 添加
    public function add()
    {
        return $this->getAuto($this->fetch(), S::goAdd(Request::post()));
    }

    // 编辑
    public function edit($id)
    {
        return $this->getAuto($this->fetch('', ['model' => M::find($id)]), S::goEdit(Request::post(), $id));
    }

    // 状态
    public function status($id)
    {
        return $this->getJson(S::goStatus(Request::post('status'), $id));
    }

    // 删除
    public function remove($id)
    {
        return $this->getJson(S::goRemove($id));
    }

    // 批量删除
    public function batchRemove()
    {
        return $this->getJson(S::goBatchRemove(Request::post('ids')));
    }

    // 回收站
    public function recycle()
    {
        return $this->getAuto($this->fetch(), S::goRecycle());
    }

    public function principalAudit()
    {
        $limit = input('get.limit');
        $admin = session('admin');
        $where[] = ['director_audit', '=', 2];
        if ($admin['school_id'] > 0) {
            $where[] = ['school_id', '=', $admin['school_id']];
        }
        $list = SchoolPettyCash::with([
            'get_admin' => function($a) {
                $a->field('id,username');
            },'get_school' => function($a) {
                $a->field('id,school_name');
            }
        ])->order('id','desc')->where($where)->paginate($limit);
        $items = $list->items();
        foreach ($items as $k => $v) {
            $users = dataFind('bl_admin_admin_role', ['admin_id'=> $admin['id']], '*', 'id desc');
            if ($users) {
                $items[$k]['group'] = AdminRole::where('id', $users['role_id'])->value('name');
            } else {
                $items[$k]['group'] = '未分组';
            }
        }
        $data = ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
        return $this->getAuto($this->fetch(), $data);
    }

    public function auditCash($id){return $this->getJson(S::auditCash(Request::param('finance_audit'),$id));}
    public function receiveCash($id){return $this->getJson(S::receiveCash(Request::param('is_receive'),$id));}

    public function investorAduit()
    {
        $limit = input('get.limit');
        $admin = session('admin');
        $where[] = ['finance_audit', '=', 2];
        if ($admin['school_id'] > 0) {
            $where[] = ['school_id', '=', $admin['school_id']];
        }
        $list = SchoolPettyCash::with([
            'get_admin' => function($a) {
                $a->field('id,username');
            },'get_school' => function($a) {
                $a->field('id,school_name');
            }
        ])->order('id','desc')->where($where)->paginate($limit);
        $items = $list->items();
        foreach ($items as $k => $v) {
            $users = dataFind('bl_admin_admin_role', ['admin_id'=> $admin['id']], '*', 'id desc');
            if ($users) {
                $items[$k]['group'] = AdminRole::where('id', $users['role_id'])->value('name');
            } else {
                $items[$k]['group'] = '未分组';
            }
        }
        $data = ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
        return $this->getAuto($this->fetch(), $data);
    }

    public function investorCash($id){return $this->getJson(S::investorCash(Request::param('finance_audit'),$id));}
}
